package com.cogent.business.helper;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.cogent.common.util.Globals;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.facade.IExpressionFacade;
import com.cogent.dynamicform.facade.impl.ExpressionFacadeImpl;

public class UserOperatorHelper {
	public static final int MOD_VIEW = 5;
	public static final int MOD_VIEW_RECYCLE = 13;
	public static final int VIEW_RESUME = 17;
	
	public static String getListOperator(HttpServletRequest request, String resourcecode, int operator, String prefix, String postfix, FormDomain formDomain) {
		StringBuffer str = new StringBuffer();
		if((operator & Globals.VIEW) == Globals.VIEW ) {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.VIEW))
				str.append("<a href=\"").append(prefix).append("_view").append(postfix).append("\">查看</a>");
			else
				str.append("查看");
		}
		if(((operator & Globals.UPDATE) == Globals.UPDATE ) && DynamicFormHelper.isNoOptModel(formDomain.getOptbtntype(), 2)) {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.UPDATE))
				str.append("　<a href=\"").append(prefix).append("_mod").append(postfix).append("\">修改</a>");
			else
				str.append("　修改");
		}
		if((operator & Globals.RESUME) == Globals.RESUME ) {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.RESUME))
				str.append("　<a href=\"javascript:resumeNew('").append(prefix).append("_resume").append(postfix).append("')\">恢复</a>");
			else
				str.append("　恢复");
		}
		if(formDomain.isIsdelete()) {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.UPDATE))
				str.append("　<a href=\"javascript:deleteNote('").append(prefix).append("_delete").append(postfix).append("')\">删除</a>");
		}
		return str.toString();
	}
	
	public static String getListOperator(HttpServletRequest request, String resourcecode, int operator, String prefix, String postfix) {
		StringBuffer str = new StringBuffer();
		if((operator & Globals.VIEW) == Globals.VIEW ) {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.VIEW))
				str.append("<a href=\"").append(prefix).append("_view").append(postfix).append("\">查看</a>");
			else
				str.append("查看");
		}
		if((operator & Globals.UPDATE) == Globals.UPDATE){
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.UPDATE))
				str.append("　<a href=\"").append(prefix).append("_mod").append(postfix).append("\">修改</a>");
			else
				str.append("　修改");
		}
		if((operator & Globals.RESUME) == Globals.RESUME ) {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.RESUME))
				str.append("　<a href=\"javascript:resumeNew('").append(prefix).append("_resume").append(postfix).append("')\">恢复</a>");
			else
				str.append("　恢复");
		}
		
		return str.toString();
	}
	
	public static String getListOperator(HttpServletRequest request, String resourcecode, int operator, String prefix, String postfix, FormDomain formDomain, HashMap dataMap) {
		if(!StringUtil.isEmpty(formDomain.getLinktaps())) {
			String linktaps = formDomain.getLinktaps();
			String[] temps = linktaps.split("\\|");
			if(temps.length > 4) {
				String[] fields = temps[1].split("~");
				postfix += "&" + fields[0] + "=" + MapUtil.getStringValue(dataMap, fields[0]);
				postfix = getPostFix(formDomain, postfix, MapUtil.getIntValue(dataMap, fields[0]));
			}
		}
		return getListOperator(request, resourcecode, operator, prefix, postfix, formDomain);
	}
	
	private static String getPostFix(FormDomain formDomain, String postfix, int curvalue) {
		String temp1 = postfix.substring(0, postfix.indexOf("&formid="));
		String id = postfix.substring(postfix.indexOf("&formid=") + "&formid=".length());
		String post = "";
		if(id.indexOf("&") > 0) {
			post = id.substring(id.indexOf("&"));
			id = id.substring(0, id.indexOf("&"));
		}
		String linktaps = formDomain.getChecklinktaps();
		String[] temps = linktaps.split("\\|");
		String[] fields = temps[0].split(";");
		for(int i=0; i<fields.length; i++) {
			String[] curtaps = fields[i].split("~");
			int curtempvalue = StringUtil.toInt(curtaps[1]);
			if(curtaps[0].equals(formDomain.getId())) {
				if(curvalue < curtempvalue) {
					String[] curtaps2 = fields[0].split("~");
					id = curtaps2[0] + "&curcode=" + formDomain.getMenuid();
					break;
				}
			}
		}
		temp1 += "&formid=" + id + post;
		
		return temp1;
	}
	
	public static String getListOptOperator(HttpServletRequest request, String resourcecode, String prefix, String postfix, FormDomain formDomain, HashMap dataMap) {
		StringBuffer str = new StringBuffer();
		if((!StringUtil.isEmpty(formDomain.getChecklinktaps())) && (formDomain.isIsmod())) {
			String linktaps = formDomain.getChecklinktaps();
			String[] temps = linktaps.split("\\|");
			String[] fields = temps[1].split("~");
			postfix += "&" + fields[0] + "=" + MapUtil.getStringValue(dataMap, fields[0]);
			String viewpostfix = getPostFix(formDomain, postfix, MapUtil.getIntValue(dataMap, fields[0]));
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.VIEW))
				str.append("<a href=\"").append(prefix).append("_view").append(viewpostfix).append("\">查看</a>");
			else
				str.append("查看");
		} else {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.VIEW))
				str.append("<a href=\"").append(prefix).append("_viewlist").append(postfix).append("\">查看</a>");
			else
				str.append("查看");
		}
		IExpressionFacade iexpressionFacade = new ExpressionFacadeImpl();
		if(StringUtil.isEmpty(formDomain.getOptprerequisites()) || (iexpressionFacade.doJudge(formDomain.getOptprerequisites(), dataMap))) {
			if((!StringUtil.isEmpty(formDomain.getChecklinktaps())) && (formDomain.isIsmod())) {
				if(UserDomainHelper.isAllow(request, resourcecode, Globals.UPDATE))
					str.append("　<a href=\"").append(prefix).append("_mod").append(postfix).append("\">").append(formDomain.getOptname()).append("</a>");
				else
					str.append("　").append(formDomain.getOptname());
			} else {
				if(UserDomainHelper.isAllow(request, resourcecode, Globals.SAVE))
					str.append("　<a href=\"").append(prefix).append("_openerlist").append(postfix).append("\">").append(formDomain.getOptname()).append("</a>");
				else
					str.append("　").append(formDomain.getOptname());
			}
		}
		if(formDomain.isIsdelete()) {
			if(UserDomainHelper.isAllow(request, resourcecode, Globals.UPDATE))
				str.append("　<a href=\"javascript:deleteNote('").append(prefix).append("_delete").append(postfix).append("')\">删除</a>");
		}
		return str.toString();
	}
}
